package org.testfire.agent;

import java.io.*;
import java.text.DateFormat;
import java.util.Date;
import java.util.Properties;
import org.testfire.agent.util.AgentPropertiesDialog;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Sep 14, 2007
 * Time: 12:59:18 PM
 */
public class AgentProperties
{

    File propertiesFile;

    File workingDirectory = new File( System.getProperty( "user.dir" ) );

    File resourcesDirectory = new File( workingDirectory, "resources" );

    File jarsDirectory = new File( resourcesDirectory, "jars" );

    File testsDirectory = new File( workingDirectory, "run" + File.separator + "src" );

    File classesDirectory = new File( workingDirectory, "run" + File.separator + "classes" );

    File saveDirectory = new File( workingDirectory, "save" );

    File root;

    Integer id = -1;

    Boolean localMode = true;

    Integer timeout = 300000;

    Integer maxThreads = 3;

    Integer maxPOSTContentLength = 100000;

    Integer listenPort = 9292;

    Boolean allowNonUserTests = true;

    String serverAddress = "http://localhost:8080/testFireServer";

    String agentName = "Some Unique Name";

    String agentAddress = "http://localhost:9292";

    String description = "Describe me!";

    String userName;
    String password;

    private static final AgentProperties instance = new AgentProperties();

    private String lastUserName = "";

    public AgentProperties()
    {

    }

    public static AgentProperties getInstance(){
        return instance;
    }

    public void writeOut() throws IOException
    {
        Properties props = new Properties( null );
        DateFormat df = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG );
        props.setProperty( "id", id.toString() );
        props.setProperty( "timeout", timeout.toString() );
        props.setProperty( "maxThreads", maxThreads.toString() );
        props.setProperty( "maxPOSTContentLength", maxPOSTContentLength.toString() );
        props.setProperty( "listenPort", listenPort.toString() );
        props.setProperty( "allowNonUserTests", allowNonUserTests.toString() );
        props.setProperty( "serverAddress", serverAddress );
        props.setProperty( "agentName", agentName );
        props.setProperty( "agentAddress", agentAddress );
        props.setProperty( "description", description );
        props.setProperty( "firstRun", String.valueOf(Boolean.FALSE));
        props.setProperty( "lastUser", lastUserName );

        OutputStream out = new FileOutputStream( propertiesFile, false );
        props.store( out, "Updated " + df.format( new Date() ) );
        out.flush();
        out.close();
    }

    public void loadProps( File propertiesFile ) throws IOException
    {
        Properties serverProperties = new Properties( null );
        this.propertiesFile = propertiesFile;

        InputStream is = new BufferedInputStream( new
            FileInputStream( propertiesFile ) );
        serverProperties.load( is );

        is.close();

        String property = serverProperties.getProperty( "firstRun" );
        boolean firstRun = Boolean.valueOf( property );
        if(firstRun)
        {
            new AgentPropertiesDialog().setVisible( true );
            return;
        }



        property = serverProperties.getProperty( "id" );
        if ( property != null )
        {
            this.id = Integer.parseInt( property );
        }

        property = serverProperties.getProperty( "lastUser" );
        if ( property != null )
        {
            this.lastUserName = property;
        }

        property = serverProperties.getProperty( "timeout" );
        if ( property != null )
        {
            this.timeout = Integer.parseInt( property );
        }
        property = serverProperties.getProperty( "maxThreads" );
        if ( property != null )
        {
            this.maxThreads = Integer.valueOf( property );
        }
        property = serverProperties.getProperty( "maxPOSTContentLength" );
        if ( property != null )
        {
            this.maxPOSTContentLength = Integer.valueOf( property );
        }
        property = serverProperties.getProperty( "listenPort" );
        if ( property != null )
        {
            this.listenPort = Integer.valueOf( property );
        }

        property = serverProperties.getProperty( "serverAddress" );
        if ( property != null )
        {
            this.serverAddress = property.endsWith( "/" )
                ? property.substring( 0, property.length() - 1 )
                : property;
        }

        property = serverProperties.getProperty( "agentName" );
        if ( property != null )
        {
            this.agentName = property;
        }

        property = serverProperties.getProperty( "agentAddress" );
        if ( property != null )
        {
            this.agentAddress = property.endsWith( "/" )
                ? property.substring( 0, property.length() - 1 )
                : property;
        }

        property = serverProperties.getProperty( "description" );
        if ( property != null )
        {
            this.description = property;
        }

        property = serverProperties.getProperty( "allowNonUserTests" );
        if ( property != null )
        {
            this.allowNonUserTests = Boolean.valueOf( property );
        }
        property = serverProperties.getProperty( "localMode" );
        if ( property != null )
        {
            this.allowNonUserTests = Boolean.valueOf( property );
        }
        if ( !testsDirectory.exists() )
        {
            testsDirectory.mkdirs();
        }
        if ( !classesDirectory.exists() )
        {
            classesDirectory.mkdirs();
        }
    }

    public File getPropertiesFile()
    {
        return propertiesFile;
    }

    public File getWorkingDirectory()
    {
        return workingDirectory;
    }

    public File getResourcesDirectory()
    {
        return resourcesDirectory;
    }

     public void setWorkingDirectory( File workingDir )
    {
        this.workingDirectory = workingDir;
        this.testsDirectory = new File(workingDir, "run" + File.separator + "src");
        this.classesDirectory = new File(workingDir, "run" + File.separator + "classes");
    }

    public File getTestSrcDirectory()
    {
        return testsDirectory;
    }

    public File getClassesDirectory()
    {
        return classesDirectory;
    }

    public File getSaveDirectory()
    {
        return saveDirectory;
    }
    
     public void setSaveDirectory( File saveDirectory )
    {
        this.saveDirectory = saveDirectory;
    }


    public Integer getId()
    {
        return id;
    }

    public Integer getTimeout()
    {
        return timeout;
    }

    public Integer getMaxThreads()
    {
        return maxThreads;
    }

    public Integer getMaxPOSTContentLength()
    {
        return maxPOSTContentLength;
    }

    public Integer getListenPort()
    {
        return listenPort;
    }

    public Boolean getAllowNonUserTests()
    {
        return allowNonUserTests;
    }

    public String getServerAddress()
    {
        return serverAddress;
    }

    public String getAgentName()
    {
        return agentName;
    }

    public String getAgentAddress()
    {
        return agentAddress;
    }

    public String getDescription()
    {
        return description;
    }

    public File getJarsDirectory()
    {
        return jarsDirectory;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword( String password )
    {
        this.password = password;
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName( String userName )
    {
        this.lastUserName = userName;
        this.userName = userName;
    }

    public Boolean getLocalMode()
    {
        return localMode;
    }

    public void setLocalMode( Boolean localMode )
    {
        this.localMode = localMode;
    }

    public void setAgentName( String agentName ) {
        this.agentName = agentName;
    }

    public void setAgentAddress( String agentAddress )
    {
        this.agentAddress = agentAddress;
    }

    public void setDescription( String description )
    {
        this.description = description;
    }

    public void setListenPort( Integer listenPort )
    {
        this.listenPort = listenPort;
    }

    public void setServerAddress( String serverAddress )
    {
        this.serverAddress = serverAddress;
    }

    public String getLastUserName()
    {
        return lastUserName;
    }
}
